<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="//cdn.jsdelivr.net/npm/phaser@4.0.0-rc.1/dist/phaser.js"></script>
    <script src="../dist/iife/spine-phaser-v4.js"></script>
    <link rel="stylesheet" href="../../index.css" />
    <title>Spine Phaser Example</title>
  </head>

  <body class="p-4 flex flex-col items-center">
    <h1>Events example</h1>
    <div style="display: flex; flex-direction: column; width: 800px">
      <canvas id="game" width="800" height="600"></canvas>
      <textarea id="log" style="height: 10em"></textarea>
    </div>
  </body>
  <script>
    function log(message) {
      let log = document.querySelector("#log");
      log.textContent += message + "\n";
      log.scrollTop = log.scrollHeight;
      console.log(message);
    }

    class EventsExample extends Phaser.Scene {
      preload() {
        this.load.spineBinary("spineboy-data", "/assets/spineboy-pro.skel");
        this.load.spineAtlas("spineboy-atlas", "/assets/spineboy-pma.atlas");
      }

      create() {
        const spineboy = this.add.spine(
          400,
          500,
          "spineboy-data",
          "spineboy-atlas"
        );
        spineboy.scale = 0.5;

        spineboy.animationState.addListener({
          start: (entry) => log(`Started animation ${entry.animation.name}`),
          interrupt: (entry) =>
            log(`Interrupted animation ${entry.animation.name}`),
          end: (entry) => log(`Ended animation ${entry.animation.name}`),
          dispose: (entry) => log(`Disposed animation ${entry.animation.name}`),
          complete: (entry) =>
            log(`Completed animation ${entry.animation.name}`),
        });

        spineboy.animationState.setAnimation(0, "walk", true);
        const trackEntry = spineboy.animationState.addAnimation(
          0,
          "run",
          true,
          3,
        );
        trackEntry.listener = {
          event: (entry, event) =>
            log(`Custom event for ${entry.animation.name}: ${event.data.name}`),
        };
      }
    }

    const config = {
      canvas: document.querySelector("#game"),
      width: 800,
      height: 600,
      type: Phaser.WEBGL,
      scene: [EventsExample],
      plugins: {
        scene: [
          {
            key: "spine.SpinePlugin",
            plugin: spine.SpinePlugin,
            mapping: "spine",
          },
        ],
      },
    };

    const game = new Phaser.Game(config);
  </script>
</html>
